package com.javenwong.android.engine;

import java.lang.reflect.Method;

import android.app.Activity;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

public class GameActivity extends Activity {

	public static AssetManager assetManager;
	public static Resources resources;
	public static GameActivity gameActivity;
	public static boolean SDK16 = true;
	
	protected GameCanvas gameCanvas;
	
	public GameActivity(){
		gameActivity = this;
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        assetManager = getAssets();
        resources = getResources();
        
        gameCanvas = new GameCanvas(this);
        
        onSetContentView();
        
        try {
        	Method method = MotionEvent.class.getMethod("getX",
					new Class[] { int.class });
			if (method!=null){
				SDK16 = false;
			}
		} catch (Exception e) {
			
		}
    }
    
    protected void onSetContentView(){
    	setContentView(gameCanvas);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	
    	if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction()==KeyEvent.ACTION_DOWN) {
			onReturnDown();
		}else{
			return super.onKeyDown(keyCode, event);
		}
		
		return true;
    }
    
    /**
     * 用户点击 手机的 返回 键
     */
    public void onReturnDown() {
    	View topView = gameCanvas.getTopView();
    	if (topView.getParentView()!=null){
			gameCanvas.closeTopView();
		}else{
			topView.back();
		}
    }
    
    /**
     * 彻底退出程序
     */
    public void exit(){
    	GameActivity.gameActivity.finish();
		System.exit(0);
    }
}